﻿app.controller("productController", function ($scope, productService, $window, categoryService) {
    categoryService.getList().then(function (results) {
        $scope.categoryList = results.data;
    }, function (error) {
        console.log(error.data.messege);
    });

    $scope.hola = function() {
        console.log($scope.name);
    }
    $scope.list = [];

    $scope.lis = function() {
        productService.getList().then(function (results){
            $scope.list = results.data;
        }, function (error) {
            console.log(error.data.messege);
        });
    }
    $scope.getById = function (product){
        $scope.product = product;
    };
    $scope.save = function () {
        $window.confirm('Se guardo exitosamente');
        if($scope.product.id){
            productService.update({ id: ""}, $scope .product).then(function (results){
                console.log(results.data);             
                $scope.product = [];
                $scope.lis();
            }, function (error){
                console.log(error.data.messege);
            });
        } else {
            productService.create($scope.product).then(function (results){
                console.log(results.data);
                $scope.product = [];
                $scope.lis();
            }, function (error){
                console.log(error.data.messege);
            });
        }
    }
    $scope.delete = function (product) {
        if ($window.confirm('Confirm delete')){
            productService.delete({ id: product.id}).then(function (results){
                console.log(results.data);
                $scope.lis();
            }, function (error){
                console.log(error.data.messege);
            });
        }
    }

    $scope.lis();
});